/*
* @Author: apple
* @Date:   2017-01-13 15:53:10
* @Last Modified by:   apple
* @Last Modified time: 2017-01-13 15:53:25
*/

const model = require('../model');
let Users = model.Users;

var fn_index = async (ctx, next) => {
    let user = ctx.state.user;
    if (user) {
        ctx.render('room.html', {
            user: user
        });
    } else {
        ctx.response.redirect('/signin');
    }
};

var fn_signin = async (ctx, next) => {
    var
        name = ctx.request.body.name || '',
        password = ctx.request.body.password || '';
    console.log(`signin with name: ${name}, password: ${password}`);
    if (name === 'koa' && password === '12345') {
        ctx.response.body = `<h1>Welcome, ${name}!</h1>`;
    } else {
        ctx.response.body = `<h1>Login failed!</h1>
        <p><a href="/">Try again</a></p>`;
    }
};

module.exports = {
    'GET /': fn_index,
    'POST /signin': fn_signin
};
